package interview.simulate;

import java.util.*;

/**
 * 分配资源id https://blog.codefun2000.com/company/hw/23-4-26/P1230.html
 */
public class IdAllocationSystem {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int l = sc.nextInt();
        int r = sc.nextInt();
        LinkedHashSet<Integer> idLinkedSet = new LinkedHashSet<>((int) ((r-l+1)/0.75));//不支持索引访问，只能迭代器顺序访问
        for (int i = l;i <= r;i++) {
            idLinkedSet.add(i);
        }
        int t = sc.nextInt();
        for (int i = 0;i < t;i++) {
            int op = sc.nextInt();
            int num = sc.nextInt();
            switch (op) {
                case 1 :
                    if (idLinkedSet.size() >= num) { //1. 移除链表set开头几个元素
                        Iterator<Integer> iterator = idLinkedSet.iterator();
                        for (int j = 1;j <= num;j++) {
                            iterator.next(); //初始iterator是没有指向元素的，所以要先调用next()
                            iterator.remove();
                        }
                    }
                    break;
                case 2 :
                    if (num >= l && num <= r) { //2. 移除链表set中的指定元素
                        idLinkedSet.remove(num);
                    }
                    break;
                case 3 :
                    if (num >= l && num <= r) { //3. 向链表尾部添加指定元素，由于Set的特性 当元素已存在时则不会重复添加
                        idLinkedSet.add(num);
                    }
            }
        }
        Iterator<Integer> iterator = idLinkedSet.iterator();
        System.out.println(iterator.next());
    }

    public static void main2(String[] args) {
        Scanner sc = new Scanner(System.in);
        int l = sc.nextInt();
        int r = sc.nextInt();
        LinkedList<Integer> idList = new LinkedList<>();
        for (int i = l;i <= r;i++) {
            idList.add(i);
        }
        int t = sc.nextInt();
        HashSet<Integer> usedIdSet = new HashSet<>();
        for (int i = 0;i < t;i++) {
            int op = sc.nextInt();
            Integer num = sc.nextInt();
            switch (op) {
                case 1 :
                    if (idList.size() >= num) {
                        for (int j = 1;j <= num;j++) {
                            usedIdSet.add(idList.removeFirst());
                        }
                    }
                    break;
                case 2 :
                    if (!usedIdSet.contains(num) && num >= l && num <= r) {
                        idList.remove(num);//注意传入基本数据类型时会视为索引
                        usedIdSet.add(num);
                    }
                    break;
                case 3 :
                    if (usedIdSet.contains(num) && num >= l && num <= r) {
                        idList.add(num);
                        usedIdSet.remove(num);
                    }
            }
        }
        System.out.println(idList.getFirst());
    }
}
